package com.angel.web.app;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.angel.core.base.JsonEntity;
import com.angel.core.util.FileUpload;
import com.angel.core.util.JsonUtil;
import com.angel.health.BuzCircle;
import com.angel.provider.app.BuzCircleProvider;
import com.angel.provider.app.WangYiProvider;
import com.github.pagehelper.StringUtil;

/**
 * @author 作者姓名 cheng
 * @version 创建时间：2016年9月19日 下午2:38:30 类说明 医师端健康圈
 */
@Controller
@RequestMapping("app/circle")
public class AppCircleController {

	@Autowired
	private BuzCircleProvider buzCircleProvider;
	@Autowired
	private WangYiProvider wyProvider;

	/**
	 * 查看所有健康圈
	 * 
	 * @param buzCircle
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "index", produces = "application/json; charset=UTF-8")
	public String index(@ModelAttribute("buzCircle") BuzCircle buzCircle) {
		buzCircle.setEnable(1);
		List<Map<String, Object>> list = buzCircleProvider.findAll(buzCircle);
		return JsonUtil.toJson(new JsonEntity("200", list));
	}

	/**
	 * 新增圈子
	 * 
	 * @param buzCircle
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "addCircle", produces = "application/json; charset=UTF-8")
	public String addCircle(@ModelAttribute("buzCircle") BuzCircle buzCircle, HttpServletRequest req) {
		if(StringUtil.isEmpty(buzCircle.getTname()) || StringUtil.isEmpty(buzCircle.getMobile()) || StringUtil.isEmpty(buzCircle.getMsg()) || buzCircle.getMagree() == null || buzCircle.getJoinmode() == null){
			return JsonUtil.toJson(new JsonEntity("-1", "参数错误！"));
		}

		try {
			MultipartHttpServletRequest request = (MultipartHttpServletRequest) req;
			MultiValueMap<String, MultipartFile> multiFileMap = request.getMultiFileMap();
			String uploadUrl;
			if (multiFileMap != null) {
				uploadUrl = FileUpload.uploadForFastdfs(multiFileMap);
				buzCircle.setIcon(uploadUrl);
			}
		} catch (Exception e) {
		}
		try {
			String createTeam = wyProvider.createTeam(JsonUtil.toJson(buzCircle));
			if (!StringUtils.isEmpty(createTeam)) {
				Map<String, String> map = JsonUtil.jsonStringToMap(createTeam);
				if ("200".equals(map.get("code"))) {
					buzCircle.setTid(map.get("tid"));
					int insert = buzCircleProvider.insert(buzCircle);
					if (insert > 0) {
						return JsonUtil.toJson(new JsonEntity("200", map.get("tid")));
					} else {
						return JsonUtil.toJson(new JsonEntity("-1", "已存在！"));
					}
				}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			return JsonUtil.toJson(new JsonEntity("-1", "出错了！"));
		}
		return JsonUtil.toJson(new JsonEntity("-1", "出错了！"));
	}

	/**
	 * 删除圈子 逻辑删除
	 * 
	 * @param buzCircle
	 * @return
	 */
	@RequestMapping(value = "delCircle", produces = "application/json; charset=UTF-8")
	@ResponseBody
	public String delCircle(@ModelAttribute("buzCircle") BuzCircle buzCircle) {
		// int delById = buzCircleProvider.delById(buzCircle);
		int delById = buzCircleProvider.delete(buzCircle);
		if (delById > 0) {
			return JsonUtil.toJson(new JsonEntity("200", "已删除"));
		}
		return JsonUtil.toJson(new JsonEntity("-1", "删除失败"));
	}

	/**
	 * 云信删除圈子
	 * 
	 * @param buzCircle
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "removeCircle", produces = "application/json; charset=UTF-8")
	public String removeCircle(@ModelAttribute("buzCircle") BuzCircle buzCircle) {
		try {
			String removeTeam = wyProvider.removeTeam(JsonUtil.toJson(buzCircle));
			if (!StringUtils.isEmpty(removeTeam)) {
				Map<String, String> map = JsonUtil.jsonStringToMap(removeTeam);
				if ("200".equals(map.get("code"))) {
					int delById = buzCircleProvider.delete(buzCircle);
					if (delById > 0) {
						return JsonUtil.toJson(new JsonEntity("200", "已删除"));
					}
					return JsonUtil.toJson(new JsonEntity("-1", "删除失败"));
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			return JsonUtil.toJson(new JsonEntity("-1", "删除失败"));
		}
		return JsonUtil.toJson(new JsonEntity("-1", "删除失败"));
	}

	/**
	 * 云信编辑圈子
	 * 
	 * @param buzCircle
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "updateCircle", produces = "application/json; charset=UTF-8")
	public String updateCircle(@ModelAttribute("buzCircle") BuzCircle buzCircle, HttpServletRequest req) {
		if (StringUtil.isEmpty(buzCircle.getMobile()) || StringUtil.isEmpty(buzCircle.getTid())) {
			return JsonUtil.toJson(new JsonEntity("-1", "参数错误！"));
		}

		try {
			MultipartHttpServletRequest request = (MultipartHttpServletRequest) req;
			MultiValueMap<String, MultipartFile> multiFileMap = request.getMultiFileMap();
			String uploadUrl;
			if (multiFileMap != null) {
				uploadUrl = FileUpload.uploadForFastdfs(multiFileMap);
				buzCircle.setIcon(uploadUrl);
			}
		} catch (Exception e) {
		}
		try {
			String updateTeam = wyProvider.updateTeam(JsonUtil.toJson(buzCircle));
			if (!StringUtils.isEmpty(updateTeam)) {
				Map<String, String> map = JsonUtil.jsonStringToMap(updateTeam);
				if ("200".equals(map.get("code"))) {
					BuzCircle circle = new BuzCircle();
					circle.setTid(buzCircle.getTid());
					List<Map<String, Object>> list = buzCircleProvider.findAll(circle);
					Integer id = (Integer) list.get(0).get("id");
					buzCircle.setId(id);
					int up = buzCircleProvider.updateById(buzCircle);
					if (up > 0) {
						return JsonUtil.toJson(new JsonEntity("200", "修改成功"));
					} else {
						return JsonUtil.toJson(new JsonEntity("-1", "失败！"));
					}
				}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			return JsonUtil.toJson(new JsonEntity("-1", "出错了！"));
		}
		return JsonUtil.toJson(new JsonEntity("-1", "出错了！"));
	}
}
